//
//  CharacterClassController.m
//  D&D Manager
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "CharacterClassController.h"


@implementation CharacterClassController
-(id)init{
	[super init];
	[NSBundle loadNibNamed:@"Character Class View"
					 owner:self];
	[characterClassController setAutomaticallyPreparesContent:NO];
	[characterClassController prepareContent];
	[characterClassController setAutomaticallyPreparesContent:YES];
	[characterClassController setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]]];
	return self;
}
-(IBAction)addNewCharacterClass:(id)sender{
	NSString *name=@"Unnamed class";
	NSManagedObject *cClass=[characterClassController newObject];
	[characterClassController addObject:cClass];
	NSArray *array=[characterClassController arrangedObjects];
	if([[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==\"%@\"",name]]] count]>1){
		BOOL goOn=NO;
		int j=2;
		while(!goOn){
			if([[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==\"%@ #%d\"",name,j]]] count]>0){
				j++;
			}else{
				[cClass setValue:[NSString stringWithFormat:@"%@ #%d",name,j] forKey:@"name"];
				goOn=YES;
				break;
			}
		}
	}
}
-(void)addNewCharacterClassWithName:(NSString *)name{
	if([[[characterClassController arrangedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==%@",name]]] count]>0){
		NSLog(@"Character Class with given name already exists, so a new one will not be made");
		return;
	}
	NSManagedObject *cClass=[characterClassController newObject];
	[cClass setValue:name
			  forKey:@"name"];
	[characterClassController addObject:cClass];
}
-(IBAction)castsSpellsChanged:(id)sender{
	if([castsSpellsOption state]==1){
		[minSpellLevelTF setBackgroundColor:[NSColor whiteColor]];
		[maxSpellLevelTF setBackgroundColor:[NSColor whiteColor]];
		[minSpellLevelTF setEnabled:YES];
		[maxSpellLevelTF setEnabled:YES];
	}else{
		//[minSpellLevelTF setStringValue:@" "];
		//[maxSpellLevelTF setStringValue:@" "];
		[minSpellLevelTF setBackgroundColor:[NSColor colorWithDeviceRed:0.93
																  green:0.93
																   blue:0.93
																  alpha:1.0]];
		[maxSpellLevelTF setBackgroundColor:[NSColor colorWithDeviceRed:0.93
																  green:0.93
																   blue:0.93
																  alpha:1.0]];
		[minSpellLevelTF setEnabled:NO];
		[maxSpellLevelTF setEnabled:NO];
	}
}
-(NSWindow *)ccWindow{
	return ccWindow;
}
-(IBAction)closeCCWindow:(id)sender{
	[ccWindow orderOut:self];
	[NSApp endSheet:ccWindow];
	[closeButton setTitle:@"Close"];
}
-(void)controlTextDidEndEditing:(NSNotification *)aNotification{
	//check name is different from existing.
	NSString *name=[[aNotification object] stringValue];
	NSManagedObject *cClass=[[characterClassController selectedObjects] objectAtIndex:0];
	NSArray *array=[characterClassController arrangedObjects];
	if([[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==\"%@\"",name]]] count]>1){
		BOOL goOn=NO;
		int j=2;
		while(!goOn){
			if([[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"name==\"%@ #%d\"",name,j]]] count]>0){
				j++;
			}else{
				[cClass setValue:[NSString stringWithFormat:@"%@ #%d",name,j] forKey:@"name"];
				goOn=YES;
				break;
			}
		}
	}	
}
-(NSManagedObjectContext *)managedObjectContext{
	return [[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];
}
-(void)setCloseButtonName:(NSString *)string{
	[closeButton setTitle:string];
}
-(void)tableViewSelectionDidChange:(NSNotification *)notification{
	if([[notification object] isEqualTo:characterClasses]){
		[self castsSpellsChanged:nil];
	}
}
@end

